<?php
class controller_module_banners extends __registry_controller {

    public $_ = array();
	
    public function __construct() {
        parent::add_object(array(
            'handle_route',
            'model_module_banners'
        ));
        $this->_layout = NULL;
    }

    public function index() {
        $this->view = new system_view();
        $html = $this->view->view_display_module('banners');
        $this->_['url_home'] = URL_BASE . $this->_seokey[0];
        $status = '';
        $userID = '';
        
        if (isset($_SESSION['Fullname']) && !empty($_SESSION['Fullname'])) {
            $this->_['use_name'] = $_SESSION['Fullname'];
        } else if (isset($_SESSION['Email']) && !empty($_SESSION['Email'])) {
            $this->_['use_name'] = $_SESSION['Email'];
        }
        if (isset($_SESSION['Email']) && !empty($_SESSION['Email'])) {
            $this->_['user_name'] = $_SESSION['Email'];
        } 
        $this->_['url_logout'] = URL_BASE . 'admin/' . $this->route->rt_get_seokey_act('logout');
        if (isset($_SESSION['ID']) && !empty($_SESSION['ID'])) {
            $url_change_pass = URL_BASE . 'admin/edit-pass/' . $_SESSION['ID'];
            $this->_['url_change_pass'] = $url_change_pass;
            $userID = $_SESSION['ID'];
        }
        $this->_['href_cancel'] = $status_href = URL_BASE . $this->_seokey[0];
        //get data new from layout
        if (isset($_POST) && !empty($_POST)) {
            
            $old_pass = isset($_POST["old_pass"]) && $_POST["old_pass"] != '' ? $_POST["old_pass"] : '';
            $new_pass = isset($_POST["new_pass"]) && $_POST["new_pass"] != '' ? $_POST["new_pass"] : '';
            $new_pass_again = isset($_POST["new_pass_again"]) && $_POST["new_pass_again"] != '' ? $_POST["new_pass_again"] : '';

            if (isset($userID) && $userID != '') {
                $user_data = $this->model__module_getUserData($userID);
            }
           
            if (isset($user_data) && $old_pass != '') {
                
                $rs = '';
                $decode_old_pass = md5(md5($old_pass));
                if ($decode_old_pass == $user_data['Password']) {
                    if ($new_pass == $new_pass_again) {
                        $rs = $this->model__module_updateUserData($userID, md5(md5($new_pass)));
                        
                        if ($rs) {
                            
                            $error = 'ok_changed';
                            $href = $this->_seokey[0];
                            $url = URL_BASE . $href;
                            $this->request->rq_goto($url);      
                        }
                    } else {
                        $error = 'pass_not_match';
                    }
                } else {
                    $error = 'pass_not_fout';
                }
               
            }
            foreach ($_POST as $key => $value) {
                if ($key == 'status') {
                    $status = $value;
                }
            }

            if ($status != '') {
                //save change staus to file lang
                $string = '<?php';
                $file_name = PATH_APP . '\\' . DIR_ADMIN . '\\view\\lang\\'.$_SESSION['dir_lang'].'\\module\\banners.php';
                //load file lang
                if (is_file("$file_name")) {
                    $inclu = include"$file_name";
                } else {
                    $inclu = false;
                }
                if ($inclu) {
                    foreach ($_ as $key => $value) {
                        if ($key == 'status') {
                            //set value new
                            $string .= "\n $" . "_['" . $key . "']='" . $status . "';";
                        } else {
                            $string .= "\n $" . "_['" . $key . "']='" . $value . "';";
                        }
                    }

                    //save file
                    if ($file_name != '') {
                        $rsfile = file_put_contents($file_name, $string);
                    }
                    if ($rsfile) {
                        $href = $this->_seokey[0];
                        $url = URL_BASE . $href;
                        $this->request->rq_goto($url);                       
                    }
                }
            }
            //unset($_POST);
        }
        return $html;
    }

}